package com.book.bridge.function;

import com.book.bridge.abst.factory.RegisterLoginComponentFactory;
import com.book.pojo.UserInfo;
import com.book.repo.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Component
public class RegisterLoginByDefault extends AbstractRegisterLoginFunc implements RegisterLoginFuncInterface{

    @Autowired
    private UserRepository userRepository;

    @Override
    public String login(String account, String password){
       /*
        UserInfo userInfo = userRepository.findByUserNameAndUserPassword(account, password);
        if(userInfo == null) {
            return "account / password ERROR!";
        }
        return "Login Success";

        */

        return super.commonLogin(account, password,userRepository);
    }
    @Override
    public String register(UserInfo userInfo){
        /*
        if(checkUserExists(userInfo.getUserName())) {
            throw new RuntimeException("User already registered.");
        }
        userInfo.setCreateDate(new Date());
        userRepository.save(userInfo);
        return "Register Success!";
         */

        return super.commonRegister(userInfo,userRepository);
    }
    @Override
    public boolean checkUserExists(String userName){
        /*
        UserInfo user = userRepository.findByUserName(userName);
        if(user == null) {
            return false;
        }
        return true;

         */

        return super.commonCheckUserExists(userName,userRepository);
    }

    /*
    @Override
    public String login3rd(HttpServletRequest request){
        return null;
    }

     */

    @PostConstruct
    private void initFuncMap(){
        RegisterLoginComponentFactory.funcMap.put("Defualt",this);
    }

}
